{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "var1.name: var1:0\n",
      "var2.name: var2:0\n",
      "var3.name: var:0\n",
      "var4.name: var_1:0\n",
      "before update var1: 1.0\n",
      "after update var1: 2.5\n",
      "before update var2: [ 1.]\n",
      "after update var2: [ 3.5]\n",
      "var5: [[-0.31131822  1.98459876  0.56532937]\n",
      " [-1.9427042   0.59924841  1.09122431]]\n",
      "var6: [[ 1.  1.]\n",
      " [ 1.  1.]]\n",
      "var7: [[ 0.]\n",
      " [ 0.]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "# var1是一个数值\n",
    "var1 = tf.get_variable(\"var1\", shape=None, initializer=tf.constant(1.0))\n",
    "# var2是一个一维数组\n",
    "var2 = tf.Variable(tf.constant(1.0, shape=[1], dtype=tf.float32), name=\"var2\")\n",
    "\n",
    "#即使设置相同的name，实际上也是不同的变量\n",
    "var3 = tf.Variable(tf.constant(1.0, shape=[1], dtype=tf.float32), name=\"var\")\n",
    "var4 = tf.Variable(1.0, name=\"var\")\n",
    "print(\"var1.name:\", var1.name)\n",
    "print(\"var2.name:\", var2.name)\n",
    "print(\"var3.name:\", var3.name)\n",
    "print(\"var4.name:\", var4.name)\n",
    "\n",
    "#变量初始化函数\n",
    "var5 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0.5, stddev=1.0, seed=1.0), name='var5')\n",
    "var6 = tf.Variable(tf.ones(shape=[2, 2]), name='var6')\n",
    "var7 = tf.Variable(tf.zeros(shape=[2, 1]), name='var7')\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    #变量赋值函数，会自动创建一个operation，必须执行operation，否则不会进行赋值\n",
    "    # var1是一个数值\n",
    "    assign_op1 = tf.assign(var1, 2.5)\n",
    "    # var2是一个一维数组\n",
    "    assign_op2 = var2.assign([3.5])\n",
    "\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    print(\"before update var1:\", var1.eval())\n",
    "    sess.run(assign_op1)\n",
    "    print(\"after update var1:\", var1.eval())\n",
    "    print(\"before update var2:\", var2.eval())\n",
    "    sess.run(assign_op2)\n",
    "    print(\"after update var2:\", var2.eval())\n",
    "    print(\"var5:\", var5.eval())\n",
    "    print(\"var6:\", var6.eval())\n",
    "    print(\"var7:\", var7.eval())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 在上下文管理器“foo”中创建变量“v”。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "code_folding": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "v.name foo/v:0\n",
      "v1.name foo/v:0\n",
      "v.name bar/v2:0\n"
     ]
    }
   ],
   "source": [
    "with tf.variable_scope(\"foo\"):\n",
    "    v = tf.get_variable(\"v\", shape=[1], initializer=tf.constant_initializer(1.0))\n",
    "                        \n",
    "#with tf.variable_scope(\"foo\"):\n",
    "   # v = tf.get_variable(\"v\", [1])\n",
    "    \n",
    "with tf.variable_scope(\"foo\", reuse=True):\n",
    "    v1 = tf.get_variable(\"v\", shape=[1])\n",
    "print(v == v1)\n",
    "print(\"v.name\", v.name)\n",
    "print(\"v1.name\", v1.name)\n",
    "\n",
    "# 如果reuse设置为True会产生错误\n",
    "with tf.variable_scope(\"bar\", reuse=False):\n",
    "    v = tf.get_variable(\"v2\", shape=[1])\n",
    "\n",
    "print(\"v.name\", v.name)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 嵌套上下文管理器中的reuse参数的使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "with tf.variable_scope(\"root\"):\n",
    "    print(tf.get_variable_scope().reuse)\n",
    "    \n",
    "    with tf.variable_scope(\"foo\", reuse=True):\n",
    "        print(tf.get_variable_scope().reuse)\n",
    "        \n",
    "        with tf.variable_scope(\"bar\"):\n",
    "            print(tf.get_variable_scope().reuse)\n",
    "            \n",
    "    print(tf.get_variable_scope().reuse)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 通过variable_scope来管理变量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "v1.name: v:0\n",
      "v2.name: foo/v:0\n",
      "v3.name: foo/bar/v:0\n",
      "v4.name: v1:0\n"
     ]
    }
   ],
   "source": [
    "v1 = tf.get_variable(\"v\", [1])\n",
    "print(\"v1.name:\", v1.name)\n",
    "\n",
    "with tf.variable_scope(\"foo\",reuse=True):\n",
    "    v2 = tf.get_variable(\"v\", [1])\n",
    "print(\"v2.name:\", v2.name)\n",
    "\n",
    "# 变量范围嵌套\n",
    "with tf.variable_scope(\"foo\"):\n",
    "    with tf.variable_scope(\"bar\"):\n",
    "        v3 = tf.get_variable(\"v\", [1])\n",
    "        print(\"v3.name:\", v3.name)\n",
    "        \n",
    "v4 = tf.get_variable(\"v1\", [1])\n",
    "print(\"v4.name:\", v4.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. 我们可以通过变量的名称来获取变量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "v5 == v3: True\n",
      "v6 == v4: True\n"
     ]
    }
   ],
   "source": [
    "with tf.variable_scope(\"\",reuse=True):\n",
    "    v5 = tf.get_variable(\"foo/bar/v\", [1])\n",
    "    print(\"v5 == v3:\", v5 == v3)\n",
    "    v6 = tf.get_variable(\"v1\", [1])     \n",
    "    print(\"v6 == v4:\", v6 == v4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
